package com.example.myfirstmobile.slice;

import com.example.myfirstmobile.ResourceTable;
import com.example.myfirstmobile.custom.data.CustomData;
import com.example.myfirstmobile.datamodel.Category;
import com.example.myfirstmobile.views.adapter.CategoryListItemProvider;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.List;

/**
 * Category List Ability Slice
 */
public class MainAbilitySlice extends AbilitySlice {
//    //设置日志的label
//    private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG"); //MY_MODULE=0x00201

    private ComponentContainer createComponent() {
        Component mainComponent = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_main_ability,
                null, false);

        List<Category> categoryList = CustomData.getCustomData(this);
        CategoryListItemProvider categoryListItemProvider = new CategoryListItemProvider(categoryList);

        ListContainer listContainer = (ListContainer) mainComponent.findComponentById(ResourceTable.Id_list_view);
        if (listContainer != null) {
            listContainer.setItemProvider(categoryListItemProvider);
            listContainer.setItemClickedListener(categoryListItemProvider);
        }

        return (ComponentContainer) mainComponent;
    }

    @Override
    public void onStart(Intent intent) {
        ComponentContainer componentContainer = createComponent();
        setUIContent(componentContainer);

        Button button = (Button) findComponentById(ResourceTable.Id_button);
// 为按钮设置点击事件回调
        button.setClickedListener(v -> {

            CustomData.getRemoteCustomData(MainAbilitySlice.this);

        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
